import Vue from 'vue'
import VueRouter from 'vue-router'
import home from 'views/home'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: home,
    meta: {
      keepalive: false
    }
  },
  // 主页
  {
    path: '/home',
    name: 'home',
    component: home,
    meta: {
      needLogin: false,
      keepalive: true
    }
  },
  // 购物车
  {
    path: '/car',
    name: 'car',
    component: () => import ('views/car'),
    meta: {
      needLogin: true,
      keepalive: false
    }
  },
  // 分类路由
  {
    path: '/cate',
    name: 'cate',
    component: () => import ('views/cate'),
    meta: {
      needLogin: false,
      keepalive: true
    }
  },
  // 我的路由
  {
    path: '/mine',
    name: 'mine',
    component: () => import ('views/mine'),
    meta: {
      needLogin: false,
      keepalive: false
    }
  },
  // 详情路由
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import ('views/Detail'),
    meta: {
      needLogin: false,
      keepalive: true
    }
  },
  // 商品列表路由
  {
    path: '/itemlist/:categoryId',
    name: '/itemlist',
    component: () => import ('views/Itemlist'),
    meta: {
      needLogin: false,
      keepalive: false
    }
  },
  // 登录页路由
  {
    path: '/login',
    name: 'login',
    component: () => import ('views/login'),
    meta: {
      keepalive: false
    }
  },
  // 订单页路由
  {
    path: '/order',
    name: 'order',
    component: () => import ('views/order'),
    meta: {
      needLogin: true,
      keepalive: false
    }
  },
  // 增加联系人页面路由
  {
    path: '/addcontent',
    name: 'addcontent',
    component: () => import ('views/addcontent'),
    meta: {
      needLogin: true,
      keepalive: false
    }
  },
  // 联系人列表页
  {
    path: '/contactlist',
    name: 'contactlist',
    component: () => import ('views/contactList'),
    meta: {
      needLogin: true,
      keepalive: false
    }
  },
  // 编辑修改联系人
  {
    path: '/contactedit/:index',
    name: 'contactedit',
    component: () => import ('views/contactedit'),
    meta: {
      needLogin: true,
      keepalive: false
    }
  },
  // 搜索页面
  {
    path: '/search',
    name: 'search',
    component: () => import ('views/search'),
    meta: {
      needLogin: false,
      keepalive: false
    }
  },
  // 显示搜索结果页
  {
    path: '/searchresult',
    name: 'searchresult',
    component: () => import ('views/searchresult'),
    meta: {
      needLogin: false,
      keepalive: false
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, form, next) => {
  if(to.fullPath=='/login' || to.fullPath == '/register') {
    next()
  }else{
    if(to.meta.needLogin){
      const tocken = localStorage.getItem('tocken')
      if(tocken){
        next()
      }else{
        alert('请先登录')
        next({
          name: 'login',
          params: {
            from: to.fullPath
          }
        })
      }
    }else{
      next()
    }
  }
})

export default router
